import '../model/health_record.dart';
import '../service/database_service.dart';
import '../service/theme_notifier.dart';
import '../service/theme_service.dart';
import '../theme/neumorphic_theme.dart';
import '../widgets/base_record_dialog.dart';
import 'package:flutter/material.dart';

class AddDiaperRecordDialog extends BaseRecordDialog {
  const AddDiaperRecordDialog({Key? key, required String babyId})
    : super(key: key, babyId: babyId);

  @override
  _AddDiaperRecordDialogState createState() => _AddDiaperRecordDialogState();
}

class _AddDiaperRecordDialogState
    extends BaseRecordDialogState<AddDiaperRecordDialog> {
  String diaperType = 'wet'; // wet, dirty, both

  @override
  String getDialogTitle() => '记录换尿布';

  @override
  Widget buildCustomContent(BuildContext context) {
    final backgroundColor = Theme.of(context).scaffoldBackgroundColor;
    final themeColors =
        ThemeService.themeColors[ThemeNotifier.instance.currentTheme]!;

    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      decoration: NeumorphicTheme.insetDecoration(
        baseColor: backgroundColor,
        borderRadius: BorderRadius.circular(16),
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              _buildTypeButton(context, 'wet', '尿尿', Icons.water_drop_outlined),
              _buildTypeButton(context, 'dirty', '便便', Icons.bathroom_outlined),
              _buildTypeButton(
                context,
                'both',
                '都有',
                Icons.check_circle_outline,
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildTypeButton(
    BuildContext context,
    String value,
    String label,
    IconData icon,
  ) {
    final backgroundColor = Theme.of(context).scaffoldBackgroundColor;
    final themeColors =
        ThemeService.themeColors[ThemeNotifier.instance.currentTheme]!;
    final isSelected = diaperType == value;

    return GestureDetector(
      onTap: () => setState(() => diaperType = value),
      child: Container(
        width: 80,
        padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
        decoration: NeumorphicTheme.surfaceDecoration(
          baseColor: backgroundColor,
          borderRadius: BorderRadius.circular(12),
          isPressed: isSelected,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(
              icon,
              color: isSelected ? themeColors.primary : Colors.grey,
              size: 24,
            ),
            const SizedBox(height: 8),
            Text(
              label,
              style: TextStyle(
                color: isSelected ? themeColors.primary : Colors.grey,
                fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                fontSize: 14,
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Future<void> saveRecord(BuildContext context) async {
    try {
      await DatabaseService().addHealthRecord(
        HealthRecord(
          babyId: widget.babyId,
          type: RecordType.diaper,
          timestamp: selectedDateTime,
          data: {'type': diaperType, 'notes': notes},
        ),
      );
      Navigator.pop(context);
    } catch (e) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(SnackBar(content: Text('保存失败: $e')));
    }
  }
}
